package com.jxw.gk;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.jxw.launcher.Common;
import com.jxw.launcher.R;
import java.util.ArrayList;

/* loaded from: classes.dex */
public class GkDragGrid extends GridView {
    private String LastAnimationID;
    private int Remainder;
    public int downX;
    public int downY;
    private View dragImageView;
    private ViewGroup dragItemView;
    int dragOffsetX;
    int dragOffsetY;
    public int dragPosition;
    private double dragScale;
    private int dropPosition;
    private int holdPosition;
    public boolean isDrag;
    private boolean isMoving;
    private int itemHeight;
    private int itemTotalCount;
    private int itemWidth;
    private Context mContext;
    private int mHorizontalSpacing;
    private OnTouchInvalidPositionListener mTouchInvalidPosListener;
    private int mVerticalSpacing;
    private Vibrator mVibrator;
    private int nColumns;
    private int nRows;
    private RelativeLayout rootLayout;
    private int startPosition;
    private int win_view_x;
    private int win_view_y;
    private WindowManager windowManager;
    private WindowManager.LayoutParams windowParams;
    public int windowX;
    public int windowY;

    /* loaded from: classes.dex */
    public interface OnTouchInvalidPositionListener {
        boolean onTouchInvalidPosition(int i);
    }

    public GkDragGrid(Context context) {
        super(context);
        this.isDrag = false;
        this.dragImageView = null;
        this.dragItemView = null;
        this.windowManager = null;
        this.windowParams = null;
        this.nColumns = 3;
        this.isMoving = false;
        this.dragScale = 1.2d;
        this.mHorizontalSpacing = 15;
        this.mVerticalSpacing = 15;
        init(context);
    }

    public GkDragGrid(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.isDrag = false;
        this.dragImageView = null;
        this.dragItemView = null;
        this.windowManager = null;
        this.windowParams = null;
        this.nColumns = 3;
        this.isMoving = false;
        this.dragScale = 1.2d;
        this.mHorizontalSpacing = 15;
        this.mVerticalSpacing = 15;
        init(context);
    }

    public GkDragGrid(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.isDrag = false;
        this.dragImageView = null;
        this.dragItemView = null;
        this.windowManager = null;
        this.windowParams = null;
        this.nColumns = 3;
        this.isMoving = false;
        this.dragScale = 1.2d;
        this.mHorizontalSpacing = 15;
        this.mVerticalSpacing = 15;
        init(context);
    }

    public void init(Context context) {
        this.mContext = context;
        this.mVibrator = (Vibrator) context.getSystemService("vibrator");
        this.mHorizontalSpacing = (int) ((this.mHorizontalSpacing * context.getResources().getDisplayMetrics().density) + 0.5f);
    }

    private int getStatusBarHeight() {
        int identifier = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            return getResources().getDimensionPixelSize(identifier);
        }
        return 0;
    }

    private boolean inRangeOfView(View view, MotionEvent motionEvent) {
        int[] iArr = new int[2];
        view.getLocationOnScreen(iArr);
        int x = (int) motionEvent.getX();
        int y = (int) motionEvent.getY();
        int i = iArr[0];
        int statusBarHeight = (int) ((iArr[1] - getStatusBarHeight()) - getResources().getDimension(R.dimen.x142));
        return x >= i && x <= i + view.getWidth() && y >= statusBarHeight && y <= statusBarHeight + view.getHeight();
    }

    @Override // android.widget.AbsListView, android.view.ViewGroup
    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == 0) {
            this.downX = (int) motionEvent.getX();
            this.downY = (int) motionEvent.getY();
            this.windowX = (int) motionEvent.getX();
            this.windowY = (int) motionEvent.getY();
            int pointToPosition = pointToPosition(this.downX, this.downY);
            if (pointToPosition != -1) {
                ImageView imageView = (ImageView) getChildAt(pointToPosition - getFirstVisiblePosition()).findViewById(R.id.delet_iv);
                if (!this.isDrag) {
                    setOnClickListener(motionEvent);
                }
            }
        }
        return super.onInterceptTouchEvent(motionEvent);
    }

    public void deleteInfo(int i) {
        DeleteAnimation(i);
    }

    public void setRelativeLayout(RelativeLayout relativeLayout) {
        this.rootLayout = relativeLayout;
    }

    public void DeleteAnimation(final int i) {
        View childAt = getChildAt(i - getFirstVisiblePosition());
        if (childAt == null) {
            return;
        }
        childAt.setDrawingCacheEnabled(true);
        Bitmap createBitmap = Bitmap.createBitmap(childAt.getDrawingCache());
        childAt.destroyDrawingCache();
        final ImageView imageView = new ImageView(this.mContext);
        imageView.setImageBitmap(createBitmap);
        imageView.setLayoutParams(new AbsListView.LayoutParams(Common.Width / 8, Common.Width / 8));
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(-2, -2);
        final int count = (getCount() - 1) - getFirstVisiblePosition();
        this.rootLayout.addView(imageView, layoutParams);
        AnimatorSet createTranslationAnim = createTranslationAnim(count, childAt, imageView);
        if (createTranslationAnim == null) {
            GkDragAdapter gkDragAdapter = (GkDragAdapter) getAdapter();
            gkDragAdapter.setHidePosition(i);
            imageView.setVisibility(8);
            imageView.clearAnimation();
            this.rootLayout.removeView(imageView);
            gkDragAdapter.deletInfo(i);
            Common.isAnimaEnd = true;
            return;
        }
        createTranslationAnim.setInterpolator(new AccelerateDecelerateInterpolator());
        createTranslationAnim.setDuration(500L);
        createTranslationAnim.addListener(new AnimatorListenerAdapter() { // from class: com.jxw.gk.GkDragGrid.1
            @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
            public void onAnimationStart(Animator animator) {
                ((GkDragAdapter) GkDragGrid.this.getAdapter()).setHidePosition(i);
                Common.isAnimaEnd = false;
            }

            @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
            public void onAnimationEnd(Animator animator) {
                imageView.setVisibility(8);
                imageView.clearAnimation();
                GkDragGrid.this.rootLayout.removeView(imageView);
                ((GkDragAdapter) GkDragGrid.this.getAdapter()).deletInfo(i);
                final ViewTreeObserver viewTreeObserver = GkDragGrid.this.getViewTreeObserver();
                viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { // from class: com.jxw.gk.GkDragGrid.1.1
                    @Override // android.view.ViewTreeObserver.OnPreDrawListener
                    public boolean onPreDraw() {
                        viewTreeObserver.removeOnPreDrawListener(this);
                        GkDragGrid.this.animateReorder(i, count);
                        return false;
                    }
                });
            }
        });
        createTranslationAnim.start();
    }

    private AnimatorSet createAnimator(View view, float f, float f2, float f3, float f4) {
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(view, "translationX", f, f2);
        ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(view, "translationY", f3, f4);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(ofFloat, ofFloat2);
        return animatorSet;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void animateReorder(int i, int i2) {
        boolean z = i2 > i;
        ArrayList arrayList = new ArrayList();
        if (z) {
            while (i < i2) {
                View childAt = getChildAt(i - getFirstVisiblePosition());
                i++;
                if (i % this.nColumns == 0) {
                    arrayList.add(createAnimator(childAt, (-childAt.getWidth()) * (this.nColumns - 1), 0.0f, childAt.getHeight(), 0.0f));
                } else {
                    arrayList.add(createAnimator(childAt, childAt.getWidth(), 0.0f, 0.0f, 0.0f));
                }
            }
        } else {
            while (i > i2) {
                View childAt2 = getChildAt(i - getFirstVisiblePosition());
                int i3 = this.nColumns;
                if ((i + i3) % i3 == 0) {
                    arrayList.add(createAnimator(childAt2, childAt2.getWidth() * (this.nColumns - 1), 0.0f, -childAt2.getHeight(), 0.0f));
                } else {
                    arrayList.add(createAnimator(childAt2, -childAt2.getWidth(), 0.0f, 0.0f, 0.0f));
                }
                i--;
            }
        }
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(arrayList);
        animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animatorSet.setDuration(300L);
        animatorSet.addListener(new AnimatorListenerAdapter() { // from class: com.jxw.gk.GkDragGrid.2
            @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
            public void onAnimationStart(Animator animator) {
            }

            @Override // android.animation.AnimatorListenerAdapter, android.animation.Animator.AnimatorListener
            public void onAnimationEnd(Animator animator) {
                Common.isAnimaEnd = true;
            }
        });
        animatorSet.start();
    }

    private AnimatorSet createTranslationAnim(int i, View view, ImageView imageView) {
        int left = view.getLeft();
        int top = view.getTop();
        View childAt = getChildAt(i);
        if (childAt != null) {
            int left2 = childAt.getLeft();
            int top2 = childAt.getTop();
            ObjectAnimator ofFloat = ObjectAnimator.ofFloat(imageView, "translationX", left, left2);
            ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(imageView, "translationY", top, top2);
            ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(imageView, "scaleX", 1.0f, 0.5f);
            ObjectAnimator ofFloat4 = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 0.5f);
            ObjectAnimator ofFloat5 = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.0f);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(ofFloat, ofFloat2, ofFloat3, ofFloat4, ofFloat5);
            return animatorSet;
        }
        return null;
    }

    public void setOnTouchInvalidPositionListener(OnTouchInvalidPositionListener onTouchInvalidPositionListener) {
        this.mTouchInvalidPosListener = onTouchInvalidPositionListener;
    }

    @Override // android.widget.AbsListView, android.view.View
    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (this.mTouchInvalidPosListener == null) {
            return super.onTouchEvent(motionEvent);
        }
        if (!isEnabled()) {
            return isClickable() || isLongClickable();
        } else if (pointToPosition((int) motionEvent.getX(), (int) motionEvent.getY()) == -1) {
            super.onTouchEvent(motionEvent);
            return this.mTouchInvalidPosListener.onTouchInvalidPosition(motionEvent.getActionMasked());
        } else {
            if (this.dragImageView != null && this.dragPosition != -1) {
                int x = (int) motionEvent.getX();
                int y = (int) motionEvent.getY();
                int action = motionEvent.getAction();
                if (action == 0) {
                    this.downX = (int) motionEvent.getX();
                    this.windowX = (int) motionEvent.getX();
                    this.downY = (int) motionEvent.getY();
                    this.windowY = (int) motionEvent.getY();
                } else if (action == 1) {
                    stopDrag();
                    onDrop(x, y);
                    requestDisallowInterceptTouchEvent(false);
                }
            }
            return super.onTouchEvent(motionEvent);
        }
    }

    public void refresh() {
        stopDrag();
        this.isDrag = false;
        GkDragAdapter gkDragAdapter = (GkDragAdapter) getAdapter();
        gkDragAdapter.setisDelete(false);
        gkDragAdapter.notifyDataSetChanged();
    }

    private void onDrag(int i, int i2, int i3, int i4) {
        View view = this.dragImageView;
        if (view != null) {
            WindowManager.LayoutParams layoutParams = this.windowParams;
            layoutParams.alpha = 0.6f;
            layoutParams.x = i3 - this.win_view_x;
            layoutParams.y = i4 - this.win_view_y;
            this.windowManager.updateViewLayout(view, layoutParams);
        }
    }

    private void onDrop(int i, int i2) {
        this.dropPosition = pointToPosition(i, i2);
        GkDragAdapter gkDragAdapter = (GkDragAdapter) getAdapter();
        gkDragAdapter.setShowDropItem(true);
        gkDragAdapter.notifyDataSetChanged();
    }

    public void setOnClickListener(final MotionEvent motionEvent) {
        setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { // from class: com.jxw.gk.GkDragGrid.3
            @Override // android.widget.AdapterView.OnItemLongClickListener
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long j) {
                return GkDragGrid.this.runLongTouch(motionEvent, i);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean runLongTouch(MotionEvent motionEvent, int i) {
        motionEvent.getX();
        motionEvent.getY();
        this.isDrag = true;
        this.startPosition = i;
        this.dragPosition = i;
        if (this.startPosition == getCount() - 1) {
            return false;
        }
        GkDragAdapter gkDragAdapter = (GkDragAdapter) getAdapter();
        gkDragAdapter.setisDelete(true);
        gkDragAdapter.notifyDataSetChanged();
        TextView textView = (TextView) ((ViewGroup) getChildAt(this.dragPosition - getFirstVisiblePosition())).findViewById(R.id.app_name);
        textView.setSelected(true);
        textView.setEnabled(false);
        if (this.dragPosition != -1) {
            requestDisallowInterceptTouchEvent(true);
            return true;
        }
        return false;
    }

    public void startDrag(Bitmap bitmap, int i, int i2) {
        stopDrag();
        this.windowParams = new WindowManager.LayoutParams();
        WindowManager.LayoutParams layoutParams = this.windowParams;
        layoutParams.gravity = 51;
        layoutParams.x = i - this.win_view_x;
        layoutParams.y = i2 - this.win_view_y;
        double d = this.dragScale;
        double width = bitmap.getWidth();
        Double.isNaN(width);
        layoutParams.width = (int) (d * width);
        WindowManager.LayoutParams layoutParams2 = this.windowParams;
        double d2 = this.dragScale;
        double height = bitmap.getHeight();
        Double.isNaN(height);
        layoutParams2.height = (int) (d2 * height);
        WindowManager.LayoutParams layoutParams3 = this.windowParams;
        layoutParams3.flags = 408;
        layoutParams3.format = -3;
        layoutParams3.windowAnimations = 0;
        ImageView imageView = new ImageView(getContext());
        imageView.setImageBitmap(bitmap);
        this.windowManager = (WindowManager) getContext().getSystemService("window");
        this.windowManager.addView(imageView, this.windowParams);
        this.dragImageView = imageView;
    }

    private void stopDrag() {
        View view = this.dragImageView;
        if (view != null) {
            this.windowManager.removeView(view);
            this.dragImageView = null;
        }
    }

    private void hideDropItem() {
        ((GkDragAdapter) getAdapter()).setShowDropItem(false);
    }

    public Animation getMoveAnimation(float f, float f2) {
        TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0f, 1, f, 1, 0.0f, 1, f2);
        translateAnimation.setFillAfter(true);
        translateAnimation.setDuration(300L);
        return translateAnimation;
    }

    /* JADX WARN: Removed duplicated region for block: B:42:0x00c6  */
    /* JADX WARN: Removed duplicated region for block: B:47:0x00cc A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public void OnMove(int r8, int r9) {
        /*
            Method dump skipped, instructions count: 216
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.jxw.gk.GkDragGrid.OnMove(int, int):void");
    }
}
